package main

import "x-go-binding.googlecode.com/hg/ui"
import "x-go-binding.googlecode.com/hg/ui/x11"
import "fmt"
import "image"
import "image/draw"
import "io/ioutil"
import "freetype-go.googlecode.com/hg/freetype"

func PutString(where ui.Window, text string) error {	
	context := freetype.NewContext()
	fontfile := "/usr/share/fonts/liberation/LiberationMono-Regular.ttf"
	fontBytes, err := ioutil.ReadFile(fontfile)
	if err != nil { fmt.Println(err); return err }
	font, err := freetype.ParseFont(fontBytes)
	if err != nil { fmt.Println(err); return err }	
	rgba := where.Screen() 
	draw.Draw(rgba, rgba.Bounds(), image.White, image.ZP, draw.Over)
	fmt.Println("draw.Drawn.")
//	
	context.SetDst(rgba)
	context.SetFont(font)
	context.SetFontSize(12)
	context.SetClip(rgba.Bounds())
	context.SetDPI(72)
	context.SetSrc(image.Black)
//
	start := freetype.Pt(120, 120) 
	_, err = context.DrawString(text, start)
	where.FlushImage()
	return err
}

func main() {
	w, err := x11.NewWindow()
	if err != nil { fmt.Println(err); return }
	draw.Draw(w.Screen(), w.Screen().Bounds(), image.White, image.ZP, draw.Over)
	events := w.EventChan()
	_ = PutString(w, "Hello World")
	for {
		e := <-events
		if e == nil { break }
		switch ee := e.(type) {
		
		case ui.MouseEvent:
			_ = ee
			// PutString(w, "Spoo!")
			
		default:
			fmt.Printf("e: type %T, value %v\n", e, e)
		}
	}
}
